<%
  game = Game.find(session[:player].game_id)
  game_setting = game.game_setting
%>
<%if game_setting.buyer_futures=='Y' || game_setting.seller_futures=='Y'%>
<%
  def cal_futures_value(contract_id)
    contract = Contract.find(contract_id)
    game = Game.find(contract.game_id)
    game_setting = game.game_setting
    if game.run_flag=='N'
      return contract.current_value
    else
      settlement_price = nil
      if game_setting.last_price
        settlement_price = game_setting.last_price
      else
        if game.current_year==1
          settlement_price = game_setting.f01_price.to_f
        else
          settlement_price = game_setting.average_price
        end
      end
      sold_price = nil
      if game.run_flag=='N'
        sold_price = (settlement_price * ((1.0 + game_setting.level1_debt_rate) ** (contract.end_year - game.current_year + 1))).round(2)
      else
        sold_price = (settlement_price * ((1.0 + game_setting.level1_debt_rate) ** (contract.end_year - game.current_year))).round(2)
      end
      val = nil
      if contract.c_type=='sell'
        val = (contract.unit_price - sold_price) * contract.total_unit
      else
        val = (sold_price - contract.unit_price) * contract.total_unit
      end
      #current_value = contract.initial_margin + val
      current_value = contract.current_value + val
      current_value = (current_value/10000.0).round * 10000
      return current_value
    end
  end

  f1 = 25.0
  if game.current_year==1
    f1 = game_setting.f01_price
  else
    f1 = game_setting.average_price * (1 + game_setting.level1_debt_rate)
  end
  f2 = f1 * (1 + game_setting.level1_debt_rate)
  f3 = f2 * (1 + game_setting.level1_debt_rate)
  f1 = f1.round(2)
  f2 = f2.round(2)
  f3 = f3.round(2)
%>
  <br/>
  <table style="border: 1px solid gray; width:90%">
    <tr align="center">
      <th></th>
      <th>สัญญา</th>
      <th>ส่งมอบปลายปีที่</th>
      <th>UnitPrice</th>
      <th>ราคาปัจจุบัน</th>
      <th>Value</th>
      <th></th>
    </tr>
    <%contracts = Contract.find(:all, :conditions=>["player_id=? AND open_flag=?", session[:player].id, 'Y'], :order=>"name, c_type")%>
    <%contracts.each do |c|%>
    <tr align="center">
      <td><%=c.name%></td>
      <td><%if c.c_type=="buy"%>ซื้อ<%else%>ขาย<%end%></td>
      <td><%=c.end_year%></td>
      <td><%=number_with_precision(c.unit_price, :precision=>2)%></td>
      <td>
        <%if game_setting.last_price%>
          <%=number_with_precision((game_setting.last_price * ((1.0 + game_setting.level1_debt_rate) ** (c.end_year - game.current_year))).round(2), :precision=>2)%>
        <%end%>
      </td>
      <td><%=number_with_delimiter(cal_futures_value(c.id))%></td>
      <td><a href="/running/close_futures2/<%=c.id%>" onclick="return confirm('ยืนยัน?')">ปิดสัญญา</a></td>
    </tr>
    <%end%>
  </table>
<%end%>